<template>
  <rx-dialog @ok="handleSubmit" @cancel="cancelFun">
    <rx-layout :leftStyle="{width:'200px'}">
      <template #left>
        <rx-category-treeeditor
            ref="categoryTree"
            cat-key="BPM"
            :statisticConfig="statisticConfig"
            :read-key="readKey"
            :edit="false"
            @handSelect="handSelect"
            :async="true"
            v-if="isShowManageTree"
            :isAdmin="isManageToTree"
            :treeRootList="treeRootList"
        ></rx-category-treeeditor>
      </template>
      <template #head>
        <div class="table-page-search-wrapper">
          <a-form>
            <a-row>
              <a-col :span="6">
                <a-form-item :label="i18n('processDef.name', '标题')">
                  <a-input :placeholder="i18n('processDef.name', '标题','input')" v-model:value="urlConfig.queryParam.Q_NAME__S_LK"/>
                </a-form-item>
              </a-col>
              <a-col  :span="6">
                <a-form-item :label="i18n('processDef.key', '编码')">
                  <a-input :placeholder="i18n('processDef.key', '编码','input')" v-model:value="urlConfig.queryParam.Q_KEY__S_LK"/>
                </a-form-item>
              </a-col>
              <a-col :span="6">
                <a-form-item  >
                  <a-button type="primary" @click="$refs.table.loadData()"><SearchOutlined/>{{$ti18('rxList.searchBtn', '查询')}}</a-button>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </template>
      <template #middle>
        <rx-fit>
          <template #middle>
            <rx-grid ref="table"
                     :hasPage="true"
                     :urlConfig="urlConfig"
                     :selectConfig="selectConfig"
                     url="/api-bpm/bpm/core/bpmDef/query"
                     field="defId"
                     :columns="columns"
                     @selectMethod="onSelectMethod"
            >
              <template #status="{row}">
                <a-tag :color="row.status==='DEPLOYED' ? 'green' : 'red'">
                  <span v-if="row.status==='DEPLOYED'">{{i18n('processDef.publish','发布')}}</span>
                  <span v-else-if="row.status==='DRAFT'">{{i18n('processDef.draft','草稿')}}</span>
                  <span v-else-if="row.status==='INVALID'">{{i18n('processDef.invalid','失效')}}</span>
                </a-tag>
              </template>
            </rx-grid>
          </template>
        </rx-fit>
      </template>
    </rx-layout>
  </rx-dialog>
</template>

<script>
import {RxLayout, Util, BaseList} from '@lowcode/jpaas-base-lib'
import { App } from '@lowcode/jpaas-share-component';
import  rxCategoryTreeeditor from "@/views/modules/column/processOverview/rx-category-treeeditor.vue"
import instAndTaskShare from "@/views/modules/column/processOverview/instAndTaskShare.js";
export default {
  name: "BpmDefDialog",
  mixins: [BaseList,instAndTaskShare,App],
  components: {
    RxLayout,rxCategoryTreeeditor
  },
  data() {
    return {
      //默认查询状态为发布的流程定义
      urlConfig: {
        ajaxType: 'post',
        contentType: 'json',
        queryParam: {
          Q_STATUS__S_EQ: "DEPLOYED",
          Q_TREE_ID__S_EQ:"",
          Q_NAME__S_LK:"",
          Q_KEY__S_LK:"",
        },
        dataField: 'result.data',
        totalField: 'result.totalCount'
      },
      selectConfig: {
        type: 'checkbox'
      },
      // 表头
      columns: [
        {
          title: this.i18n('processDef.name', '标题'),
          field: 'name',
          minWidth: 140
        },
        {
          title: this.i18n('processDef.key', '编码'),
          field: 'key',
          minWidth: 130
        },
        {
          title: this.i18n('form.status', '状态'),
          field: 'status',
          minWidth: 120,
          slots: {default: 'status'}
        },
        {
          title: this.i18n('processDef.version', '版本号'),
          field: 'version',
          minWidth: 120
        },
        {
          title: this.$ti18('rxForm.createTime', '创建时间'),
          field: 'createTime',
          minWidth: 140
        }
      ],
      statisticConfig: {
        type:"static_bpm",
        table:"BPM_DEF",
        field:"TREE_ID_",
        whereConf:[{name:"IS_MAIN_",type:"string",dateFormat:"",op:"=",value:"YES"}]
      },
      readKey:"def.read",
      isShowManageTree:false,
      isManageToTree:false,
      isBpmClient:true,
      searchName:"",
      searchKey:"",
      //弹框选择，默认可以获取到所有应用的
      isRootAdmin:true
    }
  },
  created() {
    this.urlConfig.queryParam["isRootAdmin"]=this.isRootAdmin;
  },
  methods: {
    i18n(name, text, key) {
      return this.$ti18(name, text, "BpmAgent", key);
    },
    cancelFun(){
      Util.closeWindow(this,"cancel");
    },
    handleSubmit(e) {
      var selected = [];
      if (this.selectedRows.length > 0) {
        for (var i = 0; i < this.selectedRows.length; i++) {
          var obj = {
            serial: i + 1,
            defId: this.selectedRowKeys[i],
            defName: this.selectedRows[i].name
          }
          selected.push(obj);
        }
      }
      Util.closeWindow(this, "ok", selected);
    },
    loadData() {
      if(!this.isShowManageTree){
        return;
      }
      this.$refs.table.loadData();
    },
    handSelect(curRow) {
      this.curTree = curRow;
      var treeId=curRow.treeId;
      let treeKey=this.getKeyVal(this.treePrefix,'TREE_ID__S_EQ');
      let appKey=this.getKeyVal(this.appPrefix,'APP_ID__S_EQ');
      var param={}
      this.urlConfig.queryParam[appKey]="";
      this.urlConfig.queryParam['treeCatType']="";
      this.urlConfig.queryParam['treeRootList']="";
      if (treeId!="0"){
        param[treeKey]=curRow.treeId ;
      }else {
        param[treeKey]='';
        //顶级目录（包括平台与应用目录）
        if("rootApp"!=curRow.treeCatType){
          param['treeCatType']='allRootApp';
          param['treeRootList']=JSON.stringify(this.getAppList());
        }
      }
      if("rootApp"==curRow.treeCatType){
        param = {
          'treeCatType':'customApp',
          'treeRootList':JSON.stringify(this.getAppList())
        };
        param[treeKey]='';

        if("defaultTree"==curRow.appId){
          //顶级平台目录
          param['treeCatType']='defaultApp';
        }else {
          param[appKey]=curRow.appId;
        }
      }
      this.loadDataByParam(param);
    },
    /**
     * 加载数据
     * @param parameter
     */
    loadDataByParam(parameter) {
      var params = parameter || {};
      let _queryParam = {};
      for (let key in this.urlConfig.queryParam) {//去掉前后空格；
        if (typeof this.urlConfig.queryParam[key] == "string") {
          _queryParam[key] = this.urlConfig.queryParam[key].trim();
        }
      }
      this.urlConfig.queryParam = Object.assign(_queryParam, params);
      let appId=this.curApp.appId;
      if(this.hasAppId && appId){
        this.urlConfig.queryParam.Q_APP_ID__S_EQ=appId;
      }
      this.$nextTick(() => {
        this.$refs.table.loadData();
      })
    },

  }
}
</script>

<style scoped>

</style>
